package exceptions;

/**
 * This Exception should be thrown in situations where a number is expected
 * to be zero or positive but was negative. 
 * 
 * @author Shimu
 * @date 8-May-2013
 */
public class NegativeNumberException extends Exception {
    
    public NegativeNumberException(String name, int num) {
        this(name, (long) num);
    }
    
    public NegativeNumberException(long num) {
        super(String.format("Expected a positive number, got: %d instead", num));
    }
    
    public NegativeNumberException(String name, long num) {
        super(String.format("%s should be a positive number, got: %d instead", name, num));
    }
    
    /**
     * Throws a NegativeNumberException if the given num is negative. Does 
     * nothing otherwise.
     * 
     * @param num
     * @throws NegativeNumberException 
     */
    public static void validate(int num) throws NegativeNumberException {
        validate((long) num);
    }
    
    /**
     * Throws a NegativeNumberException if the given num is negative. Does 
     * nothing otherwise.
     * 
     * @param num
     * @throws NegativeNumberException 
     */
    public static void validate(String varName, int num) throws NegativeNumberException {
        validate(varName, (long) num);
    }
    
    /**
     * Throws a NegativeNumberException if the given num is negative. Does 
     * nothing otherwise.
     * 
     * @param num
     * @throws NegativeNumberException 
     */
    public static void validate(long num) throws NegativeNumberException {
        if (num < 0) {
            throw new NegativeNumberException(num);
        }
    }
    
    /**
     * Throws a NegativeNumberException if the given num is negative. Does 
     * nothing otherwise.
     * 
     * @param num
     * @throws NegativeNumberException 
     */
    public static void validate(String varName, long num) throws NegativeNumberException {
        if (num < 0) {
            throw new NegativeNumberException(varName, num);
        }
    }
    
    
}
